package com.guli.gulimall.product.service.impl;

import com.guli.common.utils.Query;
import com.guli.gulimall.product.vo.CateLogVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;

import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> tlist() {
        List<CategoryEntity> allAlist = baseMapper.selectList(null);
        List<CategoryEntity> list = allAlist.stream().filter(item -> {
            return item.getParentCid().equals(0l);
        }).map(item -> {
            Long catId = item.getCatId();
            List<CategoryEntity> children = Children(allAlist, catId);
            item.setChildren(children);
            return item;
        }).sorted( (s1,s2)-> {
            return (s1.getSort()==null?0:s1.getSort())-(s2.getSort()==null?0:s2.getSort());
        }).collect(Collectors.toList());

        return list;
    }
    private List<CategoryEntity> Children(List<CategoryEntity> allAlist,Long id) {
        List<CategoryEntity> list = allAlist.stream().filter(item -> {
            return item.getParentCid().equals(id);
        }).map(item -> {
            Long catId = item.getCatId();
            List<CategoryEntity> children = Children(allAlist, catId);
            item.setChildren(children);
            return item;
        }).sorted( (s1,s2)-> {
            return (s1.getSort()==null?0:s1.getSort())-(s2.getSort()==null?0:s2.getSort());
        }).collect(Collectors.toList());
        return list;

    }

    @Override
    public List<CategoryEntity> treelisttwo() {
        //拿到所有的数据
        List<CategoryEntity> list = baseMapper.selectList(null);
        for (CategoryEntity categoryEntity : list) {
            //拿到二级元素
            Long catId = categoryEntity.getCatId();
            //二级元素
            List<CategoryEntity> parentCid = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId));
            categoryEntity.setChildren(parentCid);
            for (CategoryEntity entity : parentCid) {
                //三级元素
                Long catId1 = entity.getCatId();
                List<CategoryEntity> list2 = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", catId1));
                entity.setChildren(list2);
            }
        }
        return list;
    }

    @Override
    public List<CategoryEntity> getcategorylevel() {
        return baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid",0l));
    }

    @Override
    public Map<Long, List<CateLogVo>> catelogList() {
        List<CategoryEntity> allist = baseMapper.selectList(null);
        Map<Long, List<CateLogVo>> map = new HashMap<>();

        //查询一级分类
        List<CategoryEntity> collect = allist.stream().filter(item -> {
            return item.getParentCid().equals(0l);
        }).map(item -> {
            return item;
        }).collect(Collectors.toList());

        for (CategoryEntity cate : collect) {
            //所有的以及编号
            Long catId = cate.getCatId();
            //调用拿到二级和三级
            List<CateLogVo> cateLogVos = getcatelogList(catId, allist);
            map.put(catId,cateLogVos);
        }
        return map;
    }
    private List<CateLogVo> getcatelogList(Long catId, List<CategoryEntity> allist){
        List<CateLogVo> collect1 = allist.stream().filter(item -> {
            return item.getParentCid().equals(catId);
        }).map(item -> {
            //二级
            CateLogVo cateLogVo = new CateLogVo();
            cateLogVo.setCatalog1Id(item.getParentCid());
            cateLogVo.setId(item.getCatId());
            cateLogVo.setName(item.getName());

            List<CateLogVo.CateLogVo3> collect = allist.stream().filter(a -> {
                return a.getParentCid().equals(item.getCatId());
            }).map(cate -> {
                CateLogVo.CateLogVo3 cateLogVo3 = new CateLogVo.CateLogVo3();
                cateLogVo3.setCatalog2Id(cate.getParentCid());
                cateLogVo3.setId(cate.getCatId());
                cateLogVo3.setName(cate.getName());
                return cateLogVo3;
            }).collect(Collectors.toList());
            cateLogVo.setCatalog3List(collect);
            return cateLogVo;
        }).collect(Collectors.toList());
        return collect1;
    }


}